import json
import os
import re
import urlparse
from datetime import date

user = {
	'username': 'teague',
	'First Name': 'Teague',
	'Last Name': 'Sterling'
}

prefix = '/home/student/667.16'

roots = dict()
roots['~'] = {
	'name': user['First Name'],
	'path': '/home/' + user['username'],
	'alias': '~',
	'type': 'user directory root'
}
roots['Projects'] = {
	'name': 'Projects',
	'path': '/home/' + user['username'] + '/.wice-projects',
	'type': 'projects directory root',
	'parse': 'projects_directory'
}
roots['System'] = {
	'name': 'System',
	'path': '/var/www/lib',
	'type': 'system directory root'
}
roots['Public'] = {
	'name': 'Public',
	'path': '/home',
	'type': 'public directory root',
	'parse': 'public_directories'
}

query = os.environ['QUERY_STRING']

form_data = urlparse.parse_qs(query)

p = str.rstrip(re.sub('/+', '/', form_data['p'][0]), ' /')
if p == '': p = '/'

contents = []

if p == '/':
	for key, root in roots.iteritems():
		contents.append({
			'name': root['name'],
			'path': root['alias'] if 'alias' in root else root['name'],
			'type': root['type'],
			'date': date.today().strftime('%b. %d, %Y')
		})
elif p == 'Projects':
	parent = prefix + roots['Projects']['path']
	directory = os.listdir(parent)
	for entry in directory:
		if entry[0] == '.':
			continue
		path = parent + '/' + entry
		if os.path.isdir(path):
			type = 'directory'
		else:
			type = 'project'
			project = os.stat(path)

message = {
	'base': p,
	'contents': contents
}

print json.dumps(message)
